//
//  InviteFriendsVc.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/11/7.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "InviteFriendsVc.h"

@interface InviteFriendsVc ()
@property(nonatomic, strong) UIScrollView *scrollView;
@property(nonatomic, strong) UIImageView *coverImageView;
@property(nonatomic, strong) UIImageView *qrImageView;
@property(nonatomic, strong) UIButton *saveButton;
@property(nonatomic, strong) UIButton *shareButton;
@property (nonatomic, copy) NSString *code;
@property (nonatomic, copy) UIImage *codeImage;
@end

@implementation InviteFriendsVc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"邀请好友";
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.scrollView.height = self.view.height - kTabBarHeight - 60;
    self.saveButton.bottom = self.view.height - kBottomSafeHeight - 8;
    self.shareButton.bottom = self.view.height - kBottomSafeHeight - 8;
    self.saveButton.left = 16;
    self.shareButton.right = kScreenWidth - 16;
}
- (void)makeUI {
    [super makeUI];
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.coverImageView];
    [self.coverImageView addSubview:self.qrImageView];
    self.qrImageView.frame = CGRectMake(ADAPTER_WIDTH * 195, 414*ADAPTER_WIDTH, 140*ADAPTER_WIDTH, 140*ADAPTER_WIDTH);
    self.scrollView.contentSize = CGSizeMake(0, self.coverImageView.bottom);
}
- (void)handleEvent {
    [super handleEvent];
    [self.saveButton addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];
    [self.shareButton addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)bindViewModel {
    [super bindViewModel];
    WEAKSELF
    [AppGlobalCenter.share preRequestMyQr:^(NSString * _Nullable code, UIImage * _Nullable codeImage, NSError * _Nullable error) {
        weakSelf.code = code;
        weakSelf.codeImage = codeImage;
        [weakSelf buildButtons];
    }];
}

- (void)buildButtons {
    self.qrImageView.image = self.codeImage;
    [self.view addSubview:self.saveButton];
    [self.view addSubview:self.shareButton];
    self.saveButton.bottom = self.view.height - kBottomSafeHeight - 8;
    self.shareButton.bottom = self.view.height - kBottomSafeHeight - 8;
    self.saveButton.left = 16;
    self.shareButton.right = kScreenWidth - 16;
}
#pragma mark - click
- (void)share:sender {
    CommonShareModel *model = [[CommonShareModel alloc] init];
    model.url = [NSString stringWithFormat:@"%@%@?myCode=%@&myUser=%@",BA_GlobalH5_Domain,@"/download",self.code,[Utilities stringToUnicode:[TTUserModel sharedObject].nickname]];
    model.title = @"婚前婚后";
    model.content = @"婚前婚后邀您注册";
    model.image = [UIImage imageNamed:@"mainLogo"];
    [[TT_ShareManager share] callShareViewWithModel:model];
}
- (void)save:sender {
    UIGraphicsBeginImageContextWithOptions(self.coverImageView.bounds.size, 0, [[UIScreen mainScreen] scale]);
    [self.coverImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
    [SVProgressHUD showSuccessWithStatus:@"保存成功，请去相册查看"];
    [SVProgressHUD dismissWithDelay:1];
}
#pragma mark - lazy
- (UIImageView *)coverImageView {
    if (!_coverImageView) {
        UIImageView *iv = [[UIImageView alloc]init];
        iv.image = [UIImage imageNamed:ImageName.pic_yaoqing];
        iv.contentMode = UIViewContentModeScaleAspectFit;
        iv.frame = CGRectMake(16, 20, kScreenWidth - 32, ceil((kScreenWidth - 32) * 572.0 / 343.0));
        _coverImageView = iv;
    }
    return _coverImageView;
}
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        UIScrollView *view = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTabBarHeight - 60)];
        [self.view addSubview:view];
        if (@available(iOS 11.0, *)) {
            view.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        view.backgroundColor = UIColorHex(0xf7f7f7);
        _scrollView = view;
    }
    return _scrollView;
}
- (UIImageView *)qrImageView {
    if (!_qrImageView) {
        UIImageView *iv = [[UIImageView alloc]init];
        iv.contentMode = UIViewContentModeScaleAspectFit;
        _qrImageView = iv;
    }
    return _qrImageView;
}
- (UIButton *)saveButton {
    if (!_saveButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"保存海报" forState:UIControlStateNormal];
        [button setTitleColor:rgba(254, 118, 15, 1) forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        button.frame = CGRectMake(0, 0, (kScreenWidth - 44) * 0.5, 44);
        button.layer.cornerRadius = 22;
        button.layer.masksToBounds = YES;
        button.layer.borderColor = rgba(254, 118, 15, 1).CGColor;
        button.layer.borderWidth = 1;
        button.backgroundColor = UIColor.whiteColor;
        _saveButton = button;
    }
    return _saveButton;
}
- (UIButton *)shareButton {
    if (!_shareButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"分享赚钱" forState:UIControlStateNormal];
        [button setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        button.frame = CGRectMake(0, 0, (kScreenWidth - 44) * 0.5, 44);
        button.layer.cornerRadius = 22;
        button.layer.masksToBounds = YES;
        button.layer.borderColor = rgba(254, 118, 15, 1).CGColor;
        button.layer.borderWidth = 0;
        button.backgroundColor = rgba(254, 118, 15, 1);
        _shareButton = button;
    }
    return _shareButton;
}
@end
